/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[EnumTest2.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package jdk5.enums;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class EnumTest2 {

	/**
	 * 测试简单的枚举
	 */
	public static void testSize() {
		// values方法返回所有枚举的值
		System.out.println("枚举值的数目: " + Size.values().length);

		// 遍历枚举类型的所有值
		Size[] sizes = Size.values();
		for (Size size : sizes) {
			System.out.print(size + " ");
		}
		System.out.println();

		Size size = Size.SMALL;
		// 枚举值的比较使用 ==, 而不使用equals()
		if (size == Size.SMALL) {
			System.out.println("sizes' values are equal.");
		}

		// 使用valueOf方法将字符串转换为枚举值
		String str = "MEDIUM";
		size = Size.valueOf(str);
		size = Enum.valueOf(Size.class, str);
		System.out.println(size);

		// 使用ordinal方法获得枚举值在枚举类型中声明的顺序
		System.out.println("SMALL的序号:" + Size.SMALL.ordinal());
		System.out.println("MEDIUM的序号:" + Size.MEDIUM.ordinal());
		System.out.println("LARGE的序号:" + Size.LARGE.ordinal());
		System.out.println("EXTRA_LARGE的序号:" + Size.EXTRA_LARGE.ordinal());
		System.out.println();
	}

	/**
	 * 测试复杂的枚举
	 */
	public static void testComplexSize() {
		ComplexSize small = ComplexSize.SMALL;
		System.out.println(small);
		ComplexSize large = ComplexSize.LARGE;
		System.out.println(large);

		// 枚举在switch语句中的运用, Case语句直接使用枚举变量, 不需要加枚举类名
		switch (small) {
		case SMALL:
			System.out.println("小号");
			break;
		case MEDIUM:
			System.out.println("中号");
			break;
		case LARGE:
			System.out.println("大号");
			break;
		case EXTRA_LARGE:
			System.out.println("超大号");
			break;
		default:
			System.out.println("无号");
		}
	}

	public static void main(String[] args) {
		testSize();
		testComplexSize();
	}
}

/**
 * 定义一个枚举
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
enum Size {
	// 枚举值默认的修饰符是public static final
	SMALL, MEDIUM, LARGE, EXTRA_LARGE;
}

/**
 * 定义一个复杂的枚举
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
enum ComplexSize {

	SMALL("小") {
		@Override
		public String toString() {
			return "小号";
		}
	},
	MEDIUM("中"), LARGE("大"), EXTRA_LARGE;

	// 枚举类里面声明属性, 只能放在枚举的后面
	private String value = "";

	/**
	 * 默认的构造方法为private
	 */
	ComplexSize() {
	}

	/**
	 * 带参数的构造方法
	 * 
	 * @param value
	 *            枚举值
	 */
	ComplexSize(String value) {
		this.value = value;
	}

	/**
	 * 获得属性value的值
	 * 
	 * @return value的值
	 */
	public String getValue() {
		return this.value;
	}

	/**
	 * 重写toString()方法
	 * 
	 * @return 枚举值的字符串表示
	 */
	@Override
	public String toString() {
		return this.getValue();
	}
}
